package com.jiyun.springboot_user.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.jiyun.springboot_user.constans.UserConstant;
import com.jiyun.springboot_user.core.ResponseResult;
import com.jiyun.springboot_user.dao.UserDao;
import com.jiyun.springboot_user.entity.TbCount;
import com.jiyun.springboot_user.entity.User;
import com.jiyun.springboot_user.service.UserService;
import com.jiyun.springboot_user.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserDao userDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    /**
     * 注册
     *
     * @param user
     * @return
     */
    @Override
    public ResponseResult add(User user) {
        if (user.getPassword() != null && !user.getPassword().equals("")) {
            String pass = MD5Utils.encode(user.getPassword());
            user.setPassword(pass);
            user.setCreateTime(new Date());
            userDao.add(user);
            return ResponseResult.success("注册成功");
        }
        return ResponseResult.failed("密码不能为空");

    }

    /**
     * 登录
     *
     * @param user
     * @return
     */
    @Override
    public ResponseResult login(User user) {
        /**
         * 业务逻辑：
         * 先从redis中根据id取数据，redis中没有，再去查询数据库，此时需将密码加密进行判断，
         * 后将数据存入redis中，下次访问直接访问redis
         */
        if (user != null) {
            //根据ID先从redis中取数据
            String s = stringRedisTemplate.opsForValue().get(UserConstant.PREFIX_LOGIN + user.getId());
            if (!StringUtils.isEmpty(s)) {
                //将json转成对象
                User redisUser = JSONObject.parseObject(s, User.class);
                if (user.getUsername().equals(redisUser.getUsername()) && MD5Utils.encode(user.getPassword()).equals(redisUser.getPassword())) {
                    return ResponseResult.success("登录成功");
                } else {
                    return ResponseResult.failed("数据有误,登录失败");
                }
            }
            //查询数据库，先对密码进行加密
            String pass = MD5Utils.encode(user.getPassword());
            user.setPassword(pass);
            List<User> userList = userDao.login(user);
            if (CollectionUtils.isEmpty(userList) || userList.size() > 1) {
                return ResponseResult.success("登录失败");
            }

            //将用户信息保存到redis中
            User user1 = userList.get(0);
            String userJson = JSONObject.toJSONString(user1);
            stringRedisTemplate.opsForValue().set(UserConstant.PREFIX_LOGIN + user1.getId(), userJson, 30, TimeUnit.MINUTES);
            return ResponseResult.success(user.getId());


        }
        return ResponseResult.failed("数据为空，登录失败");
    }

    /**
     * 被定时调度任务
     */
    @Override
    public void selectTask() {
        //查询语句统计个数
        Integer count = userDao.selectTask();
        //直接new TbCount实体类
        TbCount tbCount = new TbCount();
        //将查询出来的count 赋值给 numbers
        tbCount.setNumbers(count);
        //时间类型
        tbCount.setExecutionTime(new Date());
        //走添加方法
        userDao.addtbUser(tbCount);
    }




}